from snownlp import seg, SnowNLP

# 引入正则表达
import re

def print_hi(name):
    print(f'Hi, {name}')

#分词
def cut_word(Text):
    seg.train('dataset/教研关键词2.txt')
    seg.save('seg.marshal')

    # 使用正则表达式去除文本中所有的换行符和空白符
    Text = re.sub('\s+', '', Text)
    # 输出正文
    # print(Text)
    s = SnowNLP(Text)
    print(s.words)



# main入口
if __name__ == '__main__':
    from snownlp import sentiment

    sentiment.train(
        r'D:\ProgramApps\Anaconda3\envs\pytorch\Lib\site-packages\snownlp\sentiment/neg.txt',
        r'D:\ProgramApps\Anaconda3\envs\pytorch\Lib\site-packages\snownlp\sentiment/pos.txt'
    )
    sentiment.save(r'E:\Coding\Python\jieba_slice_word\slice_word\dataset/sentiment.marshal.3')

    # 读取文件 encoding 参数需要根据文本文件实际的编码来设置
    with open('dataset/10月06日 - 期待.txt', 'r', encoding='utf-8') as f:
        Text = f.read()  # 读取全部内容
    f.close()  # 关闭文件，必要的操作
    cut_word(Text)
